import { defineStore } from 'pinia';

import { SuperFlag } from '@/const';
import type { UserInfo } from '@/models/UserInfo';

interface State {
  account: string;
  roles: string[];
  permissions: string[];
}

const admin = ['18181818181'];

const useAuthStore = defineStore('auth', {
  state: () => ({
    account: '',
    roles: [],
    permissions: [],
  }) as State,
  actions: {
    handleAuthInit(userInfo: UserInfo) {
      if (userInfo.superFlag === SuperFlag.SUPER) {
        if (admin.includes(userInfo.userName)) {
          this.account = userInfo.userName || '';
          this.roles = ['admin', 'ROOT'];
          this.permissions = ['ROOT'];
          return;
        }
        this.account = userInfo.userName || '';
        this.roles = ['admin'];
        this.permissions = [];
        return;
      }
      this.account = userInfo.userName || '';
      this.roles = [];
      this.permissions = [];
    },
  },
});

export default useAuthStore;
